package com.jcnet.bizaia.xstream;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;

import com.jcnet.bizaia.tools.Toolkit;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

import android.content.Context;
import android.util.Log;

/**
 * @package：com.hh.yst.allen
 * @author：Allen
 * @email：jaylong1302@163.com
 * @data：2013-4-10 下午1:13:39
 * @description：xml文档和对象之间的转换
 */
public class XmlCacheHelper {
	private File cacheDir;
	private final static String TAG = "XmlCacheHelper";
	Context context;

	public XmlCacheHelper(Context context) {
		this.context = context;
		init();
	}

	/** 初始化获取缓存目录 */
	private void init() {
		// Find the dir to save cached images
		if (android.os.Environment.getExternalStorageState().equals(
				android.os.Environment.MEDIA_MOUNTED))
			cacheDir = new File(
					android.os.Environment.getExternalStorageDirectory(),
					"Android/data/" + context.getPackageName() + "/BIZAI/");
		else
			cacheDir = context.getCacheDir();// 获取缓存目录
		if (!cacheDir.exists())
			cacheDir.mkdirs();
	}

	/** 将xml输入流缓存到文档 */
	public void inputStream2xml(String key, InputStream is) {
		// 输出到xml文档
		FileOutputStream out = null;
		try {
			File f = new File(cacheDir, key);
			out = new FileOutputStream(f);
			BufferedOutputStream bos = new BufferedOutputStream(out);
			BufferedInputStream bis = new BufferedInputStream(is);
			int len = 0;
			byte[] data = new byte[1024];
			while ((len = bis.read(data)) != -1) {
				bos.write(data, 0, len);
			}
			bos.flush();
			bos.close();
			bis.close();
			Log.e(TAG, "保存xml缓存成功");

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/** 取出本地的xml缓存 */
	public InputStream xml2Stream(String key) {
		File f = new File(cacheDir, key);
		if (f==null||!f.exists())// 如果文件不存在
			return null;
		try {
			FileInputStream fis = new FileInputStream(f);
			return fis;
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 对象转为xml文档
	 * 
	 * @param obj
	 *            对象(pojo类,list集合)
	 * @param key
	 *            键
	 */
	public void Object2Xml(Object obj, String key) {
		XStream xstream = new XStream(new DomDriver());
		// 输出到xml文档
		FileOutputStream out = null;
		try {
			File f = new File(cacheDir, key);
			if (f == null)
				return;
			out = new FileOutputStream(f);
			if (obj != null) {
				xstream.toXML(obj, out);
				Log.e(TAG, "保存xml缓存成功");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/** xml格式文档转为object对象 */
	public Object Xml2Object(String key) {
		try {
			XStream xstream = new XStream(new DomDriver());
			File f = new File(cacheDir, key);
			if (f == null || !f.exists())// 如果文件不存在
				return null;
			String xml = Toolkit
					.streamToString(new FileInputStream(f), "utf-8");
			Object obj = xstream.fromXML(xml);
			return obj;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
}
